package cn.com.dstz.admin.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import cn.com.dstz.utils.FillNameQuery;
import cn.com.dstz.base.action.BaseAction;

/**
 * 附加权限要重新设计：
 * 
 * 可以登录后台的分组：
 * 顶级居间：
 * 二级居间：
 * 喊单分析：
 * 业务员： 在公告方面， 要查询上级的公告， 又可以查看下级的公告
 * 
 * 所以， 原来的表已经不行了
 * 
 * 就如二级居间，那天也可以查看顶级居间， 又可以查看xxxxx
 * 
 *  页面的设计：
 *  			1. 你当前正在为： 分配权限。。 
 *  			2. 
 * 
 * 
 * ............................ 还有很多
 * 
 * 
 * @author 叶装装
 * 
 * 
 * 
 *
 */
public class AgentConfigAct extends BaseAction{

	public String toconfig( ){
		
		Map< String, Object > config = this.getGroupConfig( );
		
		if( config == null ){
			alertMsg( null );
			return "config";
		}
		
		getRequest( ).setAttribute( "config", config );
		
		// 查询出所有的分组信息
		setGroupList( );
		
		return "config";
	}
	
	public void config( ){
		int response = -1;
		// 添加操作
		if( saveOrUpdate == 0 ){
			response = baseService.save( "dstz_agent_config", getSaveOrUpdateOrWhereData( 0 ));
		}else{
			response = baseService.update( "dstz_agent_config", getSaveOrUpdateOrWhereData( 1 ), getSaveOrUpdateOrWhereData( 2 ) );
		}
		
		this.ajaxResponse( response > 0 ? 0 : -1 );
	}
	
	private Map< String, Object > getSaveOrUpdateOrWhereData( int type ){
		
		Map< String, Object > data = new HashMap< String, Object >( );
		if( type == 0 || type ==2 ){
			data.put( "groupid", id );
			if( type == 2 ){
				return data;
			}
		}
		
		intSameConfig( data );
		
		return data;
	}
	
	private Map< String, Object > getGroupConfig( ){
		
		if( id == null ){
			return null;
		}
		String sql = "select * from dstzgroup g, dstz_agent_config c where g.id = c.groupid and c.groupid=:groupid";
		
		Map< String, Object > config = baseService.queryForMap( sql, FillNameQuery.fillValue( sql, id ) );
		
		if( config == null || config.isEmpty( ) ){
			
			config = new HashMap< String, Object > ( );
			
			String groupSQL = "select * from dstzgroup where id=:groupid";
			Map< String, Object > group = baseService.queryForMap( groupSQL, FillNameQuery.fillValue( groupSQL, id ) ) ;
			initConfig( config, group );
		}else{
			config.put( "saveOrUpdate", 1 );
		}
		
		return config;
	}
	
	private void initConfig( Map< String, Object > config, Map< String, Object > group ){
		
		config.put( "saveOrUpdate", 0 );	// 该标识表示是应该保存还是更新
		config.put( "id", group.get( "id" ) );
		
		intSameConfig( config );
		
		config.put( "name", group.get( "name" ) );
	}
	
	private void intSameConfig( Map< String, Object > data ){
		data.put( "config_user", getConfig_user( ) );
		data.put( "config_admin", getConfig_admin( ) );
		data.put( "config_account", getConfig_account( ) );
		data.put( "config_notice", getConfig_notice( ) );
		data.put( "config_agent", getConfig_agent( ) );
		data.put( "config_category", getConfig_category( ) );
		data.put( "config_analysis", getConfig_analysis( ) );
		data.put( "config_trade_open", getConfig_trade_open( ) );
		data.put( "config_trade_close", getConfig_trade_close( ) );
		
	}
	
	private void setGroupList( ){
		
		String sql = "select * from dstzgroup g, dstz_group_type t where g.id = t.groupid and t.ismanager=:ismanager and t.level > :level";
		List< Map< String, Object >> groupList = baseService.queryForListByAll( sql, FillNameQuery.fillValue( sql, 1, 0 ) );
		
		getRequest( ).setAttribute( "groupList", groupList );
	}
	
	
	private Integer id;
	
	private String  config_user;
	private String  config_admin;
	private String  config_account;
	private String  config_notice;
	private String  config_agent;
	private String  config_category;
	private String  config_analysis;
	private String  config_trade_open;
	private String  config_trade_close;
	
	private Integer saveOrUpdate;
	
	public Integer getSaveOrUpdate( ) {
		return saveOrUpdate;
	}

	public void setSaveOrUpdate( Integer saveOrUpdate ) {
		this.saveOrUpdate = saveOrUpdate;
	}


	public Integer getId( ) {
		return id;
	}

	public void setId( Integer id ) {
		this.id = id;
	}

	public String getConfig_user( ) {
		return StringUtils.deleteWhitespace( StringUtils.trimToEmpty( config_user ) );
	}

	public void setConfig_user( String configUser ) {
		config_user = configUser;
	}

	public String getConfig_admin( ) {
		return StringUtils.deleteWhitespace( StringUtils.trimToEmpty( config_admin ) );
	}

	public void setConfig_admin( String configAdmin ) {
		config_admin = configAdmin;
	}

	public String getConfig_account( ) {
		return StringUtils.deleteWhitespace( StringUtils.trimToEmpty( config_account ) );
	}

	public void setConfig_account( String configAccount ) {
		config_account = configAccount;
	}

	public String getConfig_notice( ) {
		return StringUtils.deleteWhitespace( StringUtils.trimToEmpty( config_notice ) );
	}

	public void setConfig_notice( String configNotice ) {
		config_notice = configNotice;
	}

	public String getConfig_agent( ) {
		return StringUtils.deleteWhitespace( StringUtils.trimToEmpty( config_agent ) );
	}

	public void setConfig_agent( String configAgent ) {
		config_agent = configAgent;
	}

	public String getConfig_category( ) {
		return StringUtils.deleteWhitespace( StringUtils.trimToEmpty( config_category ) );
	}

	public void setConfig_category( String configCategory ) {
		config_category = configCategory;
	}

	public String getConfig_analysis( ) {
		return StringUtils.deleteWhitespace( StringUtils.trimToEmpty( config_analysis ) );
	}

	public void setConfig_analysis( String configAnalysis ) {
		config_analysis = configAnalysis;
	}

	public String getConfig_trade_open( ) {
		return StringUtils.deleteWhitespace( StringUtils.trimToEmpty( config_trade_open ) );
	}

	public void setConfig_trade_open( String configTradeOpen ) {
		config_trade_open = configTradeOpen;
	}

	public String getConfig_trade_close( ) {
		return StringUtils.deleteWhitespace( StringUtils.trimToEmpty( config_trade_close ) );
	}

	public void setConfig_trade_close( String configTradeClose ) {
		config_trade_close = configTradeClose;
	}


	private static final long serialVersionUID = 1L;
	

}
